﻿//Physics actor simply instantiates the Actor class and creates a PhysicsParticle, also controlls the positioning of the model according to x and y value of particle
using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Diagnostics;
namespace Organized
{

    // Manages a PhysicsObject and allows the engine to work
    // with it
    public class PhysicsActor : Actor
    {
        // The object we are managing
        public PhysicsParticle PhysicsParticle;
        Camera cam;

        Vector2 dirAngle;

        //links orientation directly to particles rotation
        public bool copyParticleAngle = false;
        //sets orientation directly to particles velocity direction
        public bool faceParticleMovement = false;
        // Override the position of the base class to that
        // of the physics object
        public Vector2 ParticlePosition
        {
            get {
                if (PhysicsParticle != null)
                    return PhysicsParticle.Position;
                else
                    return Vector2.Zero;
            }
            set
            {
                if (PhysicsParticle != null)
                    PhysicsParticle.Position = value;
            }
        }

        public override Vector3 Position
        {
            set { position = value; }
            get { return position; }
        }

        public Vector2 PositionTop{
            get { return new Vector2(modelPosition.X, modelPosition.Z); }
        }


        private Vector3 position;

        private Vector3 modelPosition;


        public PhysicsActor(Model Model,Lighting _light, PhysicsParticle _physicsParticle, Vector3 _position, GameScreen Parent)
            : base(Model,_light, _position, Parent)
        {
            this.PhysicsParticle = _physicsParticle;
            cam = Engine.Services.GetService<Camera>();
            //PhysicsParticle.Body.LinearDragCoefficient = 50f;
            //PhysicsParticle.Body.QuadraticDragCoefficient = 50f;
            PhysicsParticle.Body.RotationalDragCoefficient = 100f;
            Position = _position;
          

        }       


        // Override DisableComponent so we can remove the physics
        // object as well
        public override void DisableComponent()
        {
            this.PhysicsParticle.DisableComponent();
            base.DisableComponent();
        }
        Vector2 directionVector;

        public override void Update()
        {
            //calculate the distance to camera object when not in frustum
            //if (!inFrustum)
           // {
                distanceToCamera = Vector2.Distance(PhysicsParticle.Position, new Vector2(cam.Position.X, cam.Position.Z));
                
           //}

           // if (inFrustum)
           // {
                //set position
                modelPosition.X = ParticlePosition.X;
                modelPosition.Y = Position.Y;
                modelPosition.Z = ParticlePosition.Y;
                Position = modelPosition;

                if (copyParticleAngle)
                {
                    //set orientation according to particle

                    FacingDirection = PhysicsParticle.Rotation;
                    directionVector = MathUtil.RadianToVector2((double)FacingDirection);
                    FacingDirection = (float)MathUtil.Vector2ToRadian(new Vector2(directionVector.X, directionVector.Y));
                }
                else if (faceParticleMovement)
                {
                    //get the particles velocity angle
                   
                    dirAngle = PhysicsParticle.Body.GetVelocityAtLocalPoint(new Vector2(0, 0));
                    //invert x and y
                    directionVector.X = dirAngle.Y;
                    directionVector.Y = dirAngle.X;
                    //convert vector to radian
                    double angle = MathUtil.Vector2ToRadian(directionVector);
                    //set rotation
                    FacingDirection = (float)angle;
                }

             
           // }

            base.Update();
        }

      
    }

   
}